
#include "GenericServiceProxy.h"


IMPLEMENT_SERVICE_MODULE(GenSvcProxy, "free", "Generic Service Module.");

using namespace SAF;

#include <cassert>
#include <string>
using namespace std;


bool GenSvcProxy::Init(IKernel * kernel, IConfig * config)
{
	PERFCOUNTER("GenSvcProxy::Init");

	this->m_clientDevice = 0;
	this->m_serverDevice = 0;

	assert(kernel != NULL);
	
	/*this->m_cookieMapper = kernel->GetCookieMapper();
	assert(m_cookieMapper != NULL);*/
/*
	this->m_serverDevice = kernel->FindMessageDevice(config->GetValue("SERVERMSGDEV"));	
	if (this->m_serverDevice != NULL)
	{
		this->m_clientDevice = kernel->FindMessageDevice(config->GetValue("CLIENTMSGDEV"));
		if (this->m_clientDevice != NULL)
		{
			unsigned short servertype = 0;
			string type = config->GetValue("SERVERTYPE");
			if (type == "MC")		servertype = MC_SERVER;
			else if (type == "ACC") servertype = ACC_SERVER;
			else if (type == "ROL") servertype = ROL_SERVER;
			else if (type == "GC")	servertype = GC_SERVER;
			else if (type == "GE")	servertype = TRAN_SERVER;
			else if (type == "TRAN")servertype = GE_SERVER;	
			else if (type == "CHT") servertype = CHT_SERVER;
			else if (type == "SS")	servertype = SS_SERVER;
			else if (type == "SAF") servertype = SAF_SERVER;
			else if (type == "SAF") servertype = SAF_DISPATCHER;
			else if (type == "PCH") servertype = PCH_SERVER;
			else if (type == "CLI")	servertype = CLI_TERMINAL;
			else if (type == "NULL")servertype = NULL_SERVER;
			else 
			{				
				kernel->OutputConsole("Generic Service Device SERVERTYPE (%s) unknown.", config->GetValue("SERVERTYPE"));
				this->m_serverDevice = NULL;
				this->m_clientDevice = NULL;
				return false;
			}

			string num = config->GetValue("SERVERNUM");
			if (num == "")
			{
				kernel->OutputConsole("Warning: Generic Service Device SERVERNUM undefined. set default value 0.");
			}

			unsigned short servernum = atoi(config->GetValue("SERVERNUM"));

			kernel->OutputConsole("Generic Service Device attached to %s:%d.", config->GetValue("SERVERTYPE"), servernum);
			return true;
		}
		else
		{
			kernel->OutputConsole("Generic Service Device init failed: Outer Message Device %s not found.", config->GetValue("OUTERMSGDEV"));
			this->m_serverDevice = NULL;
			return false;
		}
	}
	else
	{
		kernel->OutputConsole("Generic Service Device init failed: Inner Message Device %s not found.", config->GetValue("INNERMSGDEV"));			
		return false;
	}
*/
	return true;
}


void GenSvcProxy::Release()
{
	this->m_serverDevice = 0;
	this->m_clientDevice = 0;
	return;
}

void GenSvcProxy::Process(SAF::IConnection * node, const SAF::NetPackage& package)
{
	PERFCOUNTER("GenSvcProxy::Process");
/*
	if (node->HostDevice() == this->m_clientDevice)		//from client to server farm
	{				
		switch(package.message.command)
		{
		case NetCommand::GAME_CHANNEL_DATA:
		case NetCommand::MSG_CLI_SAF_GAMEINTERACT_NOTIFY:
			{
				////=======================================================
				//printf("Recived %d bytes game data from user %d.\n", package.buffer.size, node->Id());
				//for (unsigned short i = 0; i < package.buffer.size; i++)
				//{
				//	putchar(((char*)package.buffer.data)[i]);
				//}
				//putchar('\n');
				////=======================================================

				//TMessage * tmsg = new TMessage();	////...............Mark
				//tmsg->Set_Server(GE_SERVER, 0);
				//tmsg->Set_ID(node->GetCookie());
				//tmsg->Set_Msg_Cmd(NetCommand::MSG_SAF_GE_GAMEINTERACT_NOTIFY);

				if ( package.buffer.size != 0) 
				{
					tmsg->Set_Body(package.buffer.data, package.buffer.size);
				}
				//this->m_serverDevice->SendNetMessage(tmsg);
			}
			return;
		case NetCommand::MSG_CLI_SAF_ENTERFIELD_ASK:
			{

				TMessage * tmsg = new TMessage();	////...............Mark
				tmsg->Set_Server(GE_SERVER, 0);
				tmsg->Set_ID(node->GetCookie());
				tmsg->Set_Msg_Cmd(NetCommand::MSG_GC_GE_ENTERFIELD_NOTIFY);
				Redun_Data_T* redun;
				GET_REDUN(*tmsg,redun);
				redun->Set_Fef_Num(0);
				redun->Set_Rol_Num(0);

				TCmd_EnterField_T askEnterField;
				askEnterField.Rol_ID = node->GetCookie();
				askEnterField.Rol_Svr_No = 0;
				_TMsgConvertor<TCmd_EnterField_T>::Set_To_Msg(askEnterField, *tmsg);
				
				//this->m_serverDevice->SendNetMessage(tmsg);
			}
			return;

		case NetCommand::MSG_CLI_SAF_EXITFIELD_ASK:
			{

				TMessage * tmsg = new TMessage();	////...............Mark
				tmsg->Set_Server(GE_SERVER, 0);
				tmsg->Set_ID(node->GetCookie());
				tmsg->Set_Msg_Cmd(NetCommand::MSG_SAF_GE_EXITFIELD_ASK);

				Redun_Data_T* redun;
				GET_REDUN(*tmsg,redun);
				redun->Set_Fef_Num(0);
				redun->Set_Rol_Num(0);

				TCmd_ExitField_T askExitField;
				askExitField.Rol_ID = node->GetCookie();
				askExitField.Rol_Svr_No = 0;
				_TMsgConvertor<TCmd_ExitField_T>::Set_To_Msg(askExitField, *tmsg);
				
				//this->m_serverDevice->SendNetMessage(tmsg);
			}
			return;

		//===============================================================
		// P2P related
		case NetCommand::P2P_TEXT_COMMAND:
			{

				TMessage * tmsg = new TMessage();	////...............Mark
				tmsg->Set_Server(P2PCTRL_SERVER, 0);
				tmsg->Set_ID(node->GetCookie());
				tmsg->Set_Msg_Cmd(NetCommand::P2P_TEXT_COMMAND);
				tmsg->Set_Body(package.buffer.data, package.buffer.size);
			
				//this->m_serverDevice->SendNetMessage(tmsg);
			}
			return;
		}
		
	}
	else if (node->HostDevice() == this->m_serverDevice)	//from server farm to client
	{
		
		//get TMessage pointor
		TMessage * pmsg = (TMessage *)package.tmsg;

//		ROLID id = pMsg->Get_ID();

		//construct a message for client		
		switch (pmsg->Get_Msg_Cmd())
		{
		case NetCommand::MSG_GE_SAF_GAMEINTERACT_NOTIFY: 
			{				
				//query the correspodent client connecton
				IConnection * node = this->m_cookieMapper->GetConnection(pmsg->Get_ID());
				if (node != NULL)
				{
					//send the message to client
					SAF::IMessageRouter::NetMessage nmsg;		
					nmsg.segment = 0;
					nmsg.command = NetCommand::GAME_CHANNEL_DATA; 
					SAF::IMessageRouter::NetBuffer buff;
					buff.data = pmsg->Get_Body();
					buff.size = pmsg->Get_Body_Size();				
					node->HostDevice()->SendNetMessage(node->Id(), nmsg, &buff, 1);
				}
				else
				{
					printf("%d user not found.", pmsg->Get_ID());
				}
				return;
			}
		case NetCommand::MSG_GE_SAF_ENTERFIELD_REPLY: 
			{
				//query the correspodent client connecton
				IConnection * node = this->m_cookieMapper->GetConnection(pmsg->Get_ID());
				if (node != NULL)
				{
					//send the message to client
					SAF::IMessageRouter::NetMessage nmsg;		
					nmsg.segment = 0;
					nmsg.command = NetCommand::MSG_SAF_CLI_ENTERFIELD_REPLY; 
					SAF::IMessageRouter::NetBuffer buff;
					buff.data = pmsg->Get_Body();
					buff.size = pmsg->Get_Body_Size();				
					node->HostDevice()->SendNetMessage(node->Id(), nmsg, &buff, 1);
				}
				else
				{
					printf("%d user not found.", pmsg->Get_ID());
				}
				return;
			}
		case NetCommand::MSG_GE_SAF_EXITFIELD_REPLY: 
			{
				//query the correspodent client connecton
				IConnection * node = this->m_cookieMapper->GetConnection(pmsg->Get_ID());
				if (node != NULL)
				{
					//send the message to client
					SAF::IMessageRouter::NetMessage nmsg;		
					nmsg.segment = 0;
					nmsg.command = NetCommand::MSG_SAF_CLI_EXITFIELD_REPLY; 
					SAF::IMessageRouter::NetBuffer buff;
					buff.data = pmsg->Get_Body();
					buff.size = pmsg->Get_Body_Size();				
					node->HostDevice()->SendNetMessage(node->Id(), nmsg, &buff, 1);
				}
				else
				{
					printf("%d user not found.", pmsg->Get_ID());
				}
				return;
			}
		case NetCommand::MSG_GE2SAF_GAMEOVER_NOTI: 
			{
				//query the correspodent client connecton
				IConnection * node = this->m_cookieMapper->GetConnection(pmsg->Get_ID());
				if (node != NULL)
				{
					//send the message to client
					SAF::IMessageRouter::NetMessage nmsg;		
					nmsg.segment = 0;
					nmsg.command = NetCommand::MSG_SAF2CLI_GAMEOVER_NOTI; 
					SAF::IMessageRouter::NetBuffer buff;
					buff.data = pmsg->Get_Body();
					buff.size = pmsg->Get_Body_Size();				
					node->HostDevice()->SendNetMessage(node->Id(), nmsg, &buff, 1);
					node->LeaveState();
					printf("GE to SAF: Game over. UserID = %d\n", pmsg->Get_ID() );
				}
				else
				{
					printf("%d user not found.", pmsg->Get_ID());
				}
				return;
			}
		//===============================================================
		// P2P related
		case NetCommand::P2P_TEXT_COMMAND:
			{
				//query the correspodent client connecton
				IConnection * node = this->m_cookieMapper->GetConnection(pmsg->Get_ID());
				if (node != NULL)
				{
					//send the message to client
					SAF::IMessageRouter::NetMessage nmsg;		
					nmsg.segment = 0;
					nmsg.command = NetCommand::P2P_TEXT_COMMAND; 
					SAF::IMessageRouter::NetBuffer buff;
					buff.data = pmsg->Get_Body();
					buff.size = pmsg->Get_Body_Size();				
					node->HostDevice()->SendNetMessage(node->Id(), nmsg, &buff, 1);
				}
			}
			return;

		default: 
			{
				assert(!"unhandled message!");	
				return;
			}
		}
		// go to next state
		//node->EnterState("LOGGED", k);	
	}
*/
	return;
}

//bool GenSvcProxy::OnException(SAF::IConnection * const node, void *&)
//{ 
//	printf("GenSvcProxy::OnException, nodeID=%lu\n",node->Id());
//	return true; 
//}


void GenSvcProxy::OnConnectionIncoming(IConnection * const node, void *& cb) 
{
	printf("GenSvcProxy::OnConnectionNodeBI, nodeID=%lu\n",node->Id());	
}

void GenSvcProxy::OnConnectionOutgoing(IConnection * const node, void *& cb) 
{
	printf("GenSvcProxy::OnConnectionNodeBI, nodeID=%lu\n",node->Id());	
}
//
//void GenSvcProxy::OnConnectionNodeBI(IConnection * const node, void *& cb) 
//{
//	printf("GenSvcProxy::OnConnectionNodeBI, nodeID=%lu\n",node->Id());	
//}
//
//void GenSvcProxy::OnConnectionNodeBO(IConnection * const node, void *& cb) 
//{
//	printf("GenSvcProxy::OnConnectionNodeBO, nodeID=%lu\n",node->Id());	
//
//}
//
//void GenSvcProxy::OnConnectionNodeFI(IConnection * const node, void *& cb) 
//{
//	printf("GenSvcProxy::OnConnectionNodeFI, nodeID=%lu\n",node->Id());	
//	//if( !this->m_cookieMapper->GetConnection(node->Id()) ){
//	//	this->m_cookieMapper->BindConnection(node->Id(), node);	
//	//	m_b4Debug=true;
//	//	printf("GenSvcProxy::OnConnectionNodeFI, trigger for debug is open!\n");	
//	//}
//}
//void GenSvcProxy::OnConnectionNodeFO(IConnection * const node, void *& cb) 
//{
//	printf("GenSvcProxy::OnConnectionNodeFO, nodeID=%lu\n", node->Id());
//
//	//if(m_b4Debug){
//	//	this->m_cookieMapper->RemoveConnection(node->Id());
//	//}
//
//	//TMessage * tmsg = new TMessage();	////...............Mark
//	//tmsg->Set_Server(GE_SERVER, 0);
//	//tmsg->Set_ID(node->Id());
//	//tmsg->Set_Msg_Cmd(NetCommand::MSG_SAF_GE_EXITFIELD_ASK);
//
//	//Redun_Data_T* redun;
//	//GET_REDUN(*tmsg,redun);
//	//redun->Set_Fef_Num(0);
//	//redun->Set_Rol_Num(0);
//
//	//TCmd_ExitField_T askExitField;
//	//askExitField.Rol_ID = node->Id();
//	//askExitField.Rol_Svr_No = 0;
//	//_TMsgConvertor<TCmd_ExitField_T>::Set_To_Msg(askExitField, *tmsg);
//
//	//this->m_serverDevice->SendNetMessage(tmsg);
//
//}
//
//
